﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using System.Windows;
using System.Linq;

namespace LogAnalizer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ctlGraph.Log = LoadLog();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //ctlGraph.Add();
            ctlGraph.Next();
        }

        private List<LogMessage> LoadLog()
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Users\Sergio\Desktop\log");
            var log = new List<LogMessage>();
            foreach (var fi in di.GetFiles())
            {
                var stream = fi.OpenText();
                while (!stream.EndOfStream)
                {
                    var json = stream.ReadLine();
                    //try
                    //{
                        var logEntry = new JavaScriptSerializer().Deserialize<LogMessage>(json);
                        log.Add(logEntry);
                    //}
                    //catch (Exception ex) { }
                }
            }

            log = log.OrderBy(x => x.time).ToList();

            return log;
        }
    }
}
